/*
Copyright (c) 2006 Peter Ritchie

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software (the "Software"), to deal in the Software for their own 
(non commercial) personal use subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Diagnostics;

using System.Windows.Forms;
using System.Drawing;

namespace MSNSearch
{
	/// <summary>
	/// TextBox that inherently handles dropping of text.
	/// </summary>
	internal class DragDroppableTextBox : TextBox
	{
		public DragDroppableTextBox ( )
		{
			this.DragOver += new DragEventHandler(DragDroppableTextBox.DragDroppableTextBox_DragOver);
			this.DragDrop += new DragEventHandler(DragDroppableTextBox.DragDroppableTextBox_DragDrop);
			this.AllowDrop = true;
#if DEBUG
			bool b = GetStyle(ControlStyles.UserMouse);
#endif
		}
		protected override void WndProc ( ref Message m )
		{
			const int WM_LBUTTONDOWN = 0x201;
			if (m.Msg == WM_LBUTTONDOWN)
			{
				if (HandleLBUTTONDOWN(m))
				{
					// was handled, don't call base.
					return;
				}
			}
			base.WndProc(ref m);
		}
		private bool DragDroppableTextBox_WM_LBUTTONDOWN ( MouseEventArgs e )
		{
			if (this.Focused)
			{
				int charIndexClicked = this.GetCharIndexFromPosition(new Point(e.X, e.Y));
				if (this.SelectionLength > 0 && (charIndexClicked >= this.SelectionStart && charIndexClicked < (this.SelectionStart + this.SelectionLength)))
				{
					this.DoDragDrop(this.SelectedText, DragDropEffects.Copy);
					return true;
				}
			}
			return false;
		}

		private bool HandleLBUTTONDOWN(Message m)
		{
			return this.DragDroppableTextBox_WM_LBUTTONDOWN(new MouseEventArgs(MouseButtons.Left, 1/*clicks*/, ((int)(long)m.LParam) & 0xffff, (short)((((int)(long)m.LParam) >> 0x10) & 0xffff), 0));
		}

		protected override void OnMouseDown ( MouseEventArgs e )
		{
			base.OnMouseDown(e);
		}

		protected static void DragDroppableTextBox_DragDrop ( object sender, DragEventArgs e )
		{
			TextBox control = sender as TextBox;
			if (control != null)
			{
				String text = e.Data.GetData(typeof(System.String)) as String;
				if (text != null)
				{
					control.SelectAll();
					control.Paste(text);
				}
			}
		}

		protected static void DragDroppableTextBox_DragOver ( object sender, DragEventArgs e )
		{
			if (e.Data.GetDataPresent(typeof(System.String)))
			{
				e.Effect = DragDropEffects.Copy;
			}
#if DEBUG
			else if (e.Data.GetDataPresent("FileDrop"))
			{
				String[] filenames = e.Data.GetData("FileDrop") as String[];
				if(filenames != null)
				{
					foreach(String filename in filenames)
					{
						Debug.WriteLine(filename);
					}
				}
			}
#endif // DEBUG
			else
			{
				e.Effect = DragDropEffects.None;
			}
		}
	}
}
